package com.shz.appletsapi.config.security.social.github.api.impl;

import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;
import org.springframework.social.oauth2.TokenStrategy;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shz.appletsapi.config.security.social.github.api.GitHub;
import com.shz.appletsapi.config.security.social.github.api.GitHubUser;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class GitHubTemplate extends AbstractOAuth2ApiBinding implements GitHub {

	private ObjectMapper objectMapper =new ObjectMapper();
	
	private static final String GET_USER_INFO="https://api.github.com/user";
	
	
	public GitHubTemplate(String accessToken) {
		super(accessToken,TokenStrategy.ACCESS_TOKEN_PARAMETER);
	}
	
	
	public GitHubTemplate() {
		super();
	}


	@Override
	public GitHubUser getGitHubUser() {
		// TODO Auto-generated method stub
		String url = GET_USER_INFO;
	    String response =(String) getRestTemplate().getForObject(url,String.class);
	    log.info("GitHub User :"+response);
	    GitHubUser userInfo = null;
	    try {
			userInfo = objectMapper.readValue(response, GitHubUser.class);
			return userInfo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("获取用户信息失败", e);
		}
	
	}

}
